<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/20 0020
 * Time: 15:55
 */

namespace app\wap\controller;


use think\Db;

class Heyue extends Vip
{
    //todo
    public function HeYueCurrency(){
        $ref = Db::name('heyue_data')
            ->field('title,platform,nowprice,time,price,cu_id')
            ->select();
        $list = [];
        $num = json_decode($this->cUrl('https://www.okex.com/api/futures/v3/instruments'));
        $week = str_replace(['2019','-'],['',''],$num[0]->delivery);
        $next_week = str_replace(['2019','-'],['',''],$num[1]->delivery);
        $jidu = str_replace(['2019','-'],['',''],$num[2]->delivery);
        foreach ($ref as $k=>$v){
            $v['user'] = 0;
            $map = ['uid'=>$this->uid,'cu_id'=>$v['cu_id'],'time'=>$v['time']];
            $user = Db::name('heyue_user')->where($map)->find();
            if($user)$v['user'] = 1;
            $v['CNY'] = $this->ChangeCNY($v['nowprice']);
            $v['lv']  = ($v['nowprice'] - $v['price'])/$v['price'];
            switch ($v['time']){
                case 1;
                    $v['times'] = $week;
                    break;
                case 2;
                    $v['times'] = $next_week;
                    break;
                case  3;
                    $v['times']=$jidu;
                    break;
            }
          	 $v['title'] = strtoupper($v['title']);
            if($v['platform'] == 'OKEx')$list['OKEx'][] = $v;
            if($v['platform'] == 'Huobi')$list['Huobi'][] = $v;
        }
        return $this->returnSuccess($list);
    }

    //todo 合约设置
    public function HeYueSet(){
      	if($this->vip == 0)return $this->returnError('请先订阅会员服务');
      	if($this->vip != 2 && $this->vip_expire < time())return $this->returnError('服务已到期,请续费');
        $map = ['uid'=>$this->uid,'cu_id'=>$this->data['cu_id'],'time'=>$this->data['time']];
        $ref = Db::name('heyue_user')->where($map)->find();
      	
        if($ref){
            $res = Db::name('heyue_user')->where($map)->delete();
        }else{
          	$count = Db::name('heyue_user')->where(['uid'=>$this->uid])->count();
          	switch($this->vip){
            	case 1;
              		if($count >= 10)return $this->returnError('提醒设置超出限制');
              		break;
              	case 2;
              		if($count >= 15)return $this->returnError('提醒设置超出限制');
              		break;
            }
          	$msg =  Db::name('message_model')->where(['id'=>2])->find();
            $data = Db::name('heyue_data')->where(['cu_id'=>$this->data['cu_id'],'time'=>$this->data['time']])->find();          	
          	if($data['time'] == 1)$data['time'] = '当周';
          	if($data['time'] == 2)$data['time'] = '次周';
          	if($data['time'] == 3)$data['time'] = '季度';
            $rea = $this->GetMessage($msg['content'],$msg['title'],$data);  
            $a = $this->SendMsg($rea,[$this->uid],6);
            $res = Db::name('heyue_user')->insert($map);
          	$reg_id = $this->GetPushReg($this->uid);
        	if($reg_id)$this->PushMsg($rea['content'],$rea['title'],$reg_id);
        }
        if($res)return $this->returnSuccess(null,'操作成功');
        return $this->returnError($this->Error);
    }


}